<script setup lang="ts">
import type { FormInstance } from 'element-plus'
import { Warning } from '@element-plus/icons-vue'
import {
  defaultInfoFormData,
  infoFormRules,
} from '../index'
import type { CustomDialogInstance } from '@/types'
import { unitManagementApi } from '@/api'
// ------------------use------------------
// #region
defineProps<{
  title: string
}>()
const emits = defineEmits<{
  success: []
}>()
const formData = defineModel('data', {
  default: () => defaultInfoFormData,
})
// #endregion
// ------------------静态变量------------------
// #region

// #endregion
// ------------------动态变量------------------
// #region

// #endregion
// ------------------ref-----------
// #region
const dialogRef = ref<CustomDialogInstance | null>(null)
const formRef = ref<FormInstance | null>(null)
// #endregion
// ------------------reactive------
// #region
// #endregion
// ------------------computed------
// #region

// #endregion
// ------------------生命周期------------------
// #region

// #endregion
// ------------------内置方法---------------------
// #region
// #endregion
// ------------------方法---------------------
// #region
async function open() {
  dialogRef.value?.open?.()
  await nextTick()
  dialogRef.value?.setSaving?.(false)
  formRef.value?.resetFields()
}
function close() {
  dialogRef.value?.close?.()
}
async function clickSave(done: () => void) {
  try {
    await formRef.value?.validate()
    await addOrUpdateData(formData.value)
    done()
    emits('success')
  }
  catch (error) {
    done()
  }
}
async function addOrUpdateData(row: any) {
  const params = { ...row }
  removeUnnecessaryParams(params)
  await unitManagementApi.saveOrUpdateUnit(params)
}
// #endregion
defineExpose({
  open,
  close,
})
</script>

<template>
  <CustomDialog
    ref="dialogRef"
    :title="title"
    need-saving
    width="600px"
    @confirm="clickSave"
  >
    <el-form
      ref="formRef"
      label-width="80px"
      label-position="right"
      :model="formData"
      :rules="infoFormRules"
      inline
    >
      <el-form-item
        label="中文名"
        prop="unitName"
        class="width-fill"
      >
        <el-input
          v-model.trim="formData.unitName"
          maxlength="10"
          placeholder="请输入中文名"
        />
      </el-form-item>
      <el-form-item
        label="英文名"
        prop="unitExpression"
        class="width-fill"
      >
        <el-input
          v-model.trim="formData.unitExpression"
          maxlength="10"
          placeholder="请输入英文名"
        />
      </el-form-item>
      <el-form-item
        label="序号"
        prop="sort"
        class="width-fill"
      >
        <template #label>
          <span class="align-center flex">
            序号
            <el-tooltip
              content="数值越小排序等级越高"
              placement="top"
            >
              <el-icon
                class="c-pointer fs-16 margin-left-xs"
              >
                <Warning />
              </el-icon>
            </el-tooltip>
          </span>
        </template>
        <el-input-number
          v-model="formData.sort"
          :min="0"
          controls-position="right"
          placeholder="请输入序号"
          class="width-fill"
        />
      </el-form-item>
    </el-form>
  </CustomDialog>
</template>

<style lang="scss" scoped>
//------------------组件样式------------------
//-------------样式穿透-------------
</style>
